package com.medical.admin.hospital.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.admin.hospital.common.dto.VisitGuideDto;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.common.vo.VisitGuideVo;
import com.medical.admin.hospital.entity.VisitGuide;
import com.medical.admin.hospital.mapper.VisitGuideMapper;
import com.medical.admin.hospital.service.VisitGuideService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

@Service
public class VisitGuideServiceImpl implements VisitGuideService {
    @Resource
    private VisitGuideMapper visitGuideMapper;

    /**
     * 分页查询就诊指南
     * @param visitGuideTitle
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<VisitGuideVo> page(String visitGuideTitle, int pageNum, int pageSize) {
        Page<VisitGuide> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<VisitGuide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(visitGuideTitle!=null,VisitGuide::getVisitGuideTitle,visitGuideTitle);
        Page<VisitGuide> visitGuidePage = visitGuideMapper.selectPage(page, queryWrapper);
        //判断数据是否为空
        if (ObjectUtil.isEmpty(visitGuidePage.getRecords())){
            throw new BaseException(ResponseCode.VISIT_GUIDE_NOT_EXIST_ERROR);
        }
        PageVo<VisitGuideVo> visitGuides = new PageVo<>();
        visitGuides.setRows(BeanConvertUtils.convertListTo(visitGuidePage.getRecords(),VisitGuideVo::new));
        visitGuides.setTotal(visitGuidePage.getRecords().size());
        return visitGuides;
    }

    /**
     * 新增就诊指南
     * @param visitGuideDto
     * @return
     */
    @Override
    public Integer insert(VisitGuideDto visitGuideDto) {
        VisitGuide visitGuide = findVisitGuide(visitGuideDto);
        //判断数据是否存在
        if (ObjectUtil.isNotEmpty(visitGuide)){
            throw new BaseException(ResponseCode.VISIT_GUIDE_EXIST_ERROR);
        }
        int count = visitGuideMapper.insert(BeanConvertUtils.convertTo(visitGuideDto, VisitGuide::new));
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 修改新增指南信息
     * @param visitGuideDto
     * @return
     */
    @Override
    public Integer update(VisitGuideDto visitGuideDto) {
        VisitGuide visitGuide = visitGuideMapper.selectById(visitGuideDto.getVisitGuideId());
        Optional.ofNullable(visitGuide).orElseThrow(()->new BaseException(ResponseCode.VISIT_GUIDE_NOT_EXIST_ERROR));
        if (visitGuide.getState()!=1){
            throw new BaseException(ResponseCode.VISIT_GUIDE_NOT_EXIST_ERROR);
        }
        return visitGuideMapper.updateById(BeanConvertUtils.convertTo(visitGuideDto,VisitGuide::new));
    }

    /**
     * 删除就诊指南信息
     * @param visitGuideId
     * @return
     */
    @Override
    public Integer delete(Long visitGuideId) {
        VisitGuide visitGuide = visitGuideMapper.selectById(visitGuideId);
        Optional.ofNullable(visitGuide).orElseThrow(()->new BaseException(ResponseCode.VISIT_GUIDE_NOT_EXIST_ERROR));
        visitGuide.setState(0);
        return visitGuideMapper.updateById(visitGuide);
    }

    /**
     * 查询就诊指南详情信息
     * @param visitGuideId
     * @return
     */
    @Override
    public VisitGuideVo select(Long visitGuideId) {
        VisitGuide visitGuide = visitGuideMapper.selectById(visitGuideId);
        if (visitGuide.getState()!=1){
            throw new BaseException(ResponseCode.VISIT_GUIDE_NOT_EXIST_ERROR);
        }
        return  BeanConvertUtils.convertTo(visitGuide,VisitGuideVo::new);
    }




    /**
     * 查询就诊指南信息
     * @param visitGuideDto
     * @return
     */
    public VisitGuide findVisitGuide(VisitGuideDto visitGuideDto){
        //通过标题判断是否存在
        LambdaQueryWrapper<VisitGuide> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VisitGuide::getVisitGuideTitle,visitGuideDto.getVisitGuideTitle());
        return visitGuideMapper.selectOne(queryWrapper);
    }
}
